import 'package:flutter/material.dart';
import 'package:xinxuan/common/address_bar.dart';
import 'package:xinxuan/common/appbar_util.dart';
import 'package:xinxuan/common/button_util.dart';
import 'package:xinxuan/common/goods_order_info_item.dart';
import 'package:xinxuan/common/network_image_util.dart';
import 'package:xinxuan/common/scroll_configuration_common.dart';
import 'package:xinxuan/controllers/my_controller/gift_controller/gift_page_controller.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';
import 'package:xinxuan/router/app_routers.dart';
import 'package:x_common_utils/utils/inkWell_util.dart';
import 'package:xinxuan/utils/color_util.dart';

class GiftPage extends StatelessWidget {

  @override
  Widget build(BuildContext context) {
    return GetBuilder<GiftPageController>(
      init: GiftPageController(),
      builder: (controller) {
        return Scaffold(
          backgroundColor: pageBgColor,
          appBar: AppBarUtil(
            title: '确认订单',
            backgroundColor: pageBgColor,
          ),
          body: controller.giftListVo.value.data!.length <= 0 ? Center(
            child: Column(
              mainAxisAlignment: MainAxisAlignment.center,
              children: [
                Image.asset(
                  'assets/icons/gift_def.png',
                  width: 273.w,
                  height: 146.h,
                ),
                Text(
                  '暂时没有可以发货的赠品~',
                  style: TextStyle(
                    color: textSubtitleColor,
                    fontSize: 14.sp
                  ),
                )
              ],
            ),
          ) : ScrollConfigurationCommon(child: ListView(
            children: [
              InkWellUtil.shakeProofInkWell(
                AddressBar(
                  consignee: controller.addressItem.value.consignee ?? '', 
                  mobile: controller.addressItem.value.mobile ?? '', 
                  provinceStr: controller.addressItem.value.provinceStr ?? '', 
                  cityStr: controller.addressItem.value.cityStr ?? '', 
                  districtStr: controller.addressItem.value.districtStr ?? '', 
                  address: controller.addressItem.value.address ?? '', 
                  id: controller.addressItem.value.id ?? 0
              ), () {
                Get.toNamed(
                  '${AppRouters.COMMON_PAGE}${AppRouters.MY_INFO_PAGE}${AppRouters.ADDRESS_LIST_PAGE}',
                  arguments: {
                    'isSelectAddress': true
                  }
                )!.then((res) {
                  if (res != null) {
                    controller.addressItem.value = res;
                    controller.update();
                  }
                });
              }),
              SizedBox(height: 12.h),
              Container(
                margin: EdgeInsets.only(top: 12.h),
                padding: EdgeInsets.symmetric(horizontal: 12.w),
                color: bgWhiteColor,
                  child: Column(
                  children: [
                    Container(
                      height: 44.h,
                      alignment: Alignment.centerLeft,
                      child: Text(
                        '魔法星选直营',
                        style: TextStyle(
                          fontSize: 14.sp,
                          color: textBlackColor,
                          fontWeight: FontWeight.bold
                        ),
                      ),
                    ),
                    _goodArrBody(controller)
                  ],
                ),
              ),
              Container(
                margin: EdgeInsets.only(top: 12.h),
                color: bgWhiteColor,
                child: Column(
                  children: [
                    Container(
                      padding: EdgeInsets.symmetric(vertical: 8.h),
                      child: Column(
                        children: [
                          GoodsOrderInfoItem(title: '商品金额', value: '¥ ${controller.giftListVo.value.data![0].price!}', valueBold: true),
                          GoodsOrderInfoItem(title: '运费', value: '¥ 0.00', valueBold: true),
                          GoodsOrderInfoItem(title: '立减', value: '- ¥ 0.00', valueBold: true),
                        ],
                      ),
                    ),
                    Container(
                      height: 49.h,
                      alignment: Alignment.centerRight,
                      padding: EdgeInsets.symmetric(horizontal: 16.w),
                      decoration: BoxDecoration(
                        border: Border(
                          top: BorderSide(
                            color: lineColor,
                            width: .5
                          )
                        )
                      ),
                      child: Text.rich(
                        TextSpan(
                          text: '合计: ',
                          style: TextStyle(
                            color: textBlackColor,
                            fontSize: 14.sp
                          ),
                          children: [
                            TextSpan(
                              text: '¥',
                              style: TextStyle(
                                color: mainTextColor,
                                fontSize: 14.sp,
                                fontWeight: FontWeight.bold
                              ),
                            ),
                            TextSpan(
                              text: '${controller.price}',
                              style: TextStyle(
                                color: mainTextColor,
                                fontSize: 18.sp,
                                fontWeight: FontWeight.bold
                              ),
                            )
                          ]
                        )
                      ),
                    )
                  ],
                ),
              ),
              Container(
                margin: EdgeInsets.only(top: 12.h),
                alignment: Alignment.center,
                height: 48.h,
                color: bgWhiteColor,
                padding: EdgeInsets.symmetric(horizontal: 16.w),
                child: Text(
                  '此商品将正常发货，除质量问题，不支持退货。',
                  style: TextStyle(
                    color: mainTextColor,
                    fontSize: 11.sp
                  ),
                ),
              )
            ],
          )),
          bottomNavigationBar: BottomAppBar(
            elevation: 0,
            child: Container(
              height: 56.h,
              padding: EdgeInsets.symmetric(horizontal: 16.w),
              child: Row(
                mainAxisAlignment: MainAxisAlignment.spaceBetween,
                children: [
                  Text.rich(
                    TextSpan(
                      text: '¥',
                      style: TextStyle(
                        color: mainTextColor,
                        fontSize: 18.sp,
                        fontWeight: FontWeight.bold
                      ),
                      children: [
                        TextSpan(
                          text: '${controller.price.value}',
                          style: TextStyle(
                            color: mainTextColor,
                            fontSize: 22.sp,
                            fontWeight: FontWeight.bold
                          ),
                        )
                      ]
                    )
                  ),
                  Container(
                    width: 120.w,
                    height: 36.h,
                    child: ButtonUtil(
                      text: '提交订单', 
                      fontSize: 14.sp, 
                      borderRadius: 18.w, 
                      height: 36.h, 
                      onTop: () {
                        controller.submitShipment();
                      }
                    ),
                  )
                ],
              ),
            ),
          ),
        );
      },
    );
  }

  /// 商品列表
  Widget _goodArrBody(GiftPageController controller) {
    return Column(
      children: controller.giftListVo.value.data!.map((productItem) {
        return Container(
          margin: EdgeInsets.only(bottom: 12.h),
          child: Row(
            crossAxisAlignment: CrossAxisAlignment.start,
            mainAxisAlignment: MainAxisAlignment.start,
            children: [
              NetworkImageUtil(
                url: productItem.image ?? '', 
                width: 90.w, 
                height: 90.w,
                borderRadius: 5.w,
              ),
              SizedBox(width: 12.w),
              Expanded(child: Container(
                constraints: BoxConstraints(
                  minHeight: 90.w,
                ),
                child: Column(
                  crossAxisAlignment: CrossAxisAlignment.start,
                  mainAxisAlignment: MainAxisAlignment.spaceBetween,
                  children: [
                    Text(
                      '${productItem.title}',
                      maxLines: 2,
                      overflow: TextOverflow.ellipsis,
                      style: TextStyle(
                        color: textBlackColor,
                        fontSize: 14.sp
                      ),
                    ),
                    Container(
                      height: 28.h,
                      child: Row(
                        mainAxisAlignment: MainAxisAlignment.spaceBetween,
                        crossAxisAlignment: CrossAxisAlignment.start,
                        children: [
                          Text(
                            '库存:${productItem.quantity}',
                            style: TextStyle(
                              color: textSubtitleColor,
                              fontSize: 12.sp
                            ),
                          ),
                          Row(
                            children: [
                              InkWellUtil.shakeProofInkWell(Image.asset(
                                'assets/icons/icon_move.png',
                                width: 30.w,
                                height: 28.h,
                                fit: BoxFit.cover,
                              ), () {
                                controller.changeQuantity(false);
                              }),
                              Container(
                                width: 36.w,
                                height: 28.h,
                                color: pageBgColor,
                                alignment: Alignment.center,
                                margin: EdgeInsets.symmetric(horizontal: 1.w),
                                child: Text(
                                  '${controller.quantity.value}',
                                  style: TextStyle(
                                    color: textBlackColor,
                                    fontSize: 12.sp
                                  ),
                                ),
                              ),
                              InkWellUtil.shakeProofInkWell(Image.asset(
                                'assets/icons/icon_add.png',
                                width: 30.w,
                                height: 28.h,
                                fit: BoxFit.cover,
                              ), () {
                                controller.changeQuantity(true);
                              })
                            ],
                          )
                        ],
                      ),
                    )
                  ],
                ),
              )),
            ],
          ),
        );
      }).toList(),
    );
  }
}